<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="简称" prop="abbreviation">
      <el-input v-model="dataForm.abbreviation" placeholder="简称"></el-input>
    </el-form-item>
    <el-form-item label="父ID" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父ID"></el-input>
    </el-form-item>
    <el-form-item label="部门类型 1为分公司，2为一级转运中心 3为二级转运中心 4为网点" prop="orgType">
      <el-input v-model="dataForm.orgType" placeholder="部门类型 1为分公司，2为一级转运中心 3为二级转运中心 4为网点"></el-input>
    </el-form-item>
    <el-form-item label="省" prop="provinceId">
      <el-input v-model="dataForm.provinceId" placeholder="省"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="cityId">
      <el-input v-model="dataForm.cityId" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="区" prop="countyId">
      <el-input v-model="dataForm.countyId" placeholder="区"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="contractNumber">
      <el-input v-model="dataForm.contractNumber" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="负责人id" prop="managerId">
      <el-input v-model="dataForm.managerId" placeholder="负责人id"></el-input>
    </el-form-item>
    <el-form-item label="树结构" prop="treePath">
      <el-input v-model="dataForm.treePath" placeholder="树结构"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sortValue">
      <el-input v-model="dataForm.sortValue" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="describe">
      <el-input v-model="dataForm.describe" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="latitude">
      <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="营业时间" prop="businessHours">
      <el-input v-model="dataForm.businessHours" placeholder="营业时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          abbreviation: '',
          parentId: '',
          orgType: '',
          provinceId: '',
          cityId: '',
          countyId: '',
          address: '',
          contractNumber: '',
          managerId: '',
          treePath: '',
          sortValue: '',
          status: '',
          describe: '',
          latitude: '',
          longitude: '',
          businessHours: ''
        },
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          abbreviation: [
            { required: true, message: '简称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父ID不能为空', trigger: 'blur' }
          ],
          orgType: [
            { required: true, message: '部门类型 1为分公司，2为一级转运中心 3为二级转运中心 4为网点不能为空', trigger: 'blur' }
          ],
          provinceId: [
            { required: true, message: '省不能为空', trigger: 'blur' }
          ],
          cityId: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          countyId: [
            { required: true, message: '区不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          contractNumber: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          managerId: [
            { required: true, message: '负责人id不能为空', trigger: 'blur' }
          ],
          treePath: [
            { required: true, message: '树结构不能为空', trigger: 'blur' }
          ],
          sortValue: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          describe: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          latitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          businessHours: [
            { required: true, message: '营业时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/auth/coreorg/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.coreOrg.name
                this.dataForm.abbreviation = data.coreOrg.abbreviation
                this.dataForm.parentId = data.coreOrg.parentId
                this.dataForm.orgType = data.coreOrg.orgType
                this.dataForm.provinceId = data.coreOrg.provinceId
                this.dataForm.cityId = data.coreOrg.cityId
                this.dataForm.countyId = data.coreOrg.countyId
                this.dataForm.address = data.coreOrg.address
                this.dataForm.contractNumber = data.coreOrg.contractNumber
                this.dataForm.managerId = data.coreOrg.managerId
                this.dataForm.treePath = data.coreOrg.treePath
                this.dataForm.sortValue = data.coreOrg.sortValue
                this.dataForm.status = data.coreOrg.status
                this.dataForm.describe = data.coreOrg.describe
                this.dataForm.latitude = data.coreOrg.latitude
                this.dataForm.longitude = data.coreOrg.longitude
                this.dataForm.businessHours = data.coreOrg.businessHours
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/auth/coreorg/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'abbreviation': this.dataForm.abbreviation,
                'parentId': this.dataForm.parentId,
                'orgType': this.dataForm.orgType,
                'provinceId': this.dataForm.provinceId,
                'cityId': this.dataForm.cityId,
                'countyId': this.dataForm.countyId,
                'address': this.dataForm.address,
                'contractNumber': this.dataForm.contractNumber,
                'managerId': this.dataForm.managerId,
                'treePath': this.dataForm.treePath,
                'sortValue': this.dataForm.sortValue,
                'status': this.dataForm.status,
                'describe': this.dataForm.describe,
                'latitude': this.dataForm.latitude,
                'longitude': this.dataForm.longitude,
                'businessHours': this.dataForm.businessHours
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
